package com.dzt.trainingmgt.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogUtils {

    // 定义不同级别的日志记录器
    private static final Logger infoLogger = LoggerFactory.getLogger("infoLogger");
    private static final Logger warnLogger = LoggerFactory.getLogger("warnLogger");
    private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
    private static final Logger fatalLogger = LoggerFactory.getLogger("fatalLogger");

    // 获取当前时间，格式为 yyyy-MM-dd HH:mm:ss
    private static String getFormattedTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    // 获取调用的类名和方法名
    private static String getLogHead() {
        StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];  // 获取调用者的堆栈信息
        String className = stackTraceElement.getClassName();  // 类名
        String methodName = stackTraceElement.getMethodName();  // 方法名
        return className + " : " + methodName;
    }

    // 记录 info 级别日志
    public static void logInfo(String message) {
        String logHead = getLogHead();
        infoLogger.info(getFormattedTime() + " - INFO - " + logHead + " : " + message);
    }

    // 记录 warn 级别日志
    public static void logWarn(String message) {
        String logHead = getLogHead();
        warnLogger.warn(getFormattedTime() + " - WARN - " + logHead + " : " + message);
    }

    // 记录 error 级别日志
    public static void logError(String message) {
        String logHead = getLogHead();
        errorLogger.error(getFormattedTime() + " - ERROR - " + logHead + " : " + message);
    }

    // 记录 fatal 级别日志
    public static void logFatal(String message) {
        String logHead = getLogHead();
        fatalLogger.error(getFormattedTime() + " - FATAL - " + logHead + " : " + message);
    }

}
